<?php

class configuration {
	protected $configs = array();
	protected $currentCat;
	protected $aether;

	public function __construct($aether) {
		$this->aether = $aether;
	}

	public function get($category, $name) {
		if(isset($this->configs[$category][$name])) {
			return $this->configs[$category][$name];
		} else {
			return null;
		}
	}

	public function loadAll(array $modules) {
		$this->load('general', $modules);
		$this->load('database', $modules);
		$this->load('globals', $modules);
		$this->load('cache', $modules);
		switch($this->configs['general']['appMode']) {
			case 'development':
				$this->configs['general']['appMode'] = AETHER_DEVELOPMENT;
				break;
			case 'staging':
				$this->configs['general']['appMode'] = AETHER_STAGING;
				break;
			case 'production':
				$this->configs['general']['appMode'] = AETHER_PRODUCTION;
				break;
		}
	}

	public function load($categoryName, $modules) {
		$this->currentCat = $categoryName;
		sins($this->configs[$categoryName], array());
		foreach($modules as $module) {
			$this->loadFile($module);
		}
	}

	public function loadFile($module) {
		if($module=='core' OR $module=='app') {
			$fn = '';
		} else {
			$fn = 'plugins/';
		}
		$fn .= $module.'/config/'.$this->currentCat;
		$fn2 = $fn.'.local.yml';
		$fn .= '.yml';
		$this->loadYaml($fn);
		$this->loadYaml($fn2);
	}

	public function loadYaml($filename) {
		if(!file_exists($filename)) {
			return false;
		}
		$data = spyc::YAMLLoad($filename);
		if(isset($data['additions'])) {
			foreach($data['additions'] as $name => $addition) {
				if(isset($this->configs[$this->currentCat][$name])) {
					$this->configs[$this->currentCat][$name] = arrayAppend($this->configs[$this->currentCat][$name], $addition);
				} else {
					$this->configs[$this->currentCat][$name] = $addition;
				}
			}
			unset($data['additions']);
		}
		foreach($data as $name => $value) {
			$this->configs[$this->currentCat][$name] = $value;
		}
	}
}

//EOF
